// pages/user/mybalance.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    userInfo: {},
    balance: 0,
    dataList: [],
    showPop: false,
    selectedAmount: null,
    amountList: [100, 200, 500, 1000, 2000, 5000],
    isShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    let meIsShow = wx.getStorageSync('meIsShow')
    console.log('123')
    //用户授权登录
    app.login(function (userInfo) {
      that.setData({
        userInfo: userInfo,
        meIsShow: meIsShow
      });
      that.getData();
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
    wx.stopPullDownRefresh();
  },
  onClickHide() {
    this.setData({
      isShow:false,
     
    });
  },

  /**
   * 获取数据
   */
  getData: function () {
    var that = this;
    var paraArr = new Array();
    paraArr['user_id'] = that.data.userInfo.userID;
    paraArr['type'] = 'balance';
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api/comm_get/getPayLog',
      data: {
        user_id: paraArr['user_id'],
        type: paraArr['type'],
        sign: sign
      },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          dataList: res.data.list,
          balance: res.data.balance,
          hiddenLoading: true
        });
      }
    })
  },

  onClickHides(){},
  // 金额选择
  selectAmount(e) {
    const amount = parseInt(e.currentTarget.dataset.amount)
    this.setData({
      selectedAmount: this.data.selectedAmount === amount ? null : amount,
      m_total:amount
    })
  },
  /*
   *弹窗
   */
  recharge: function () {
    var that = this;
    that.setData({
      isShow: !that.data.isShow
    });
    wx.requestSubscribeMessage({
      tmplIds: ["puN0n1YgDwtOp88rk_BkmyY0LSfi-jaHupYpiwKqUMQ"],
      success(res) {

      },
      fail(res) {
        console.log(res, 'res')
        wx.showModal({
          title: '提示',
          content: res.errMsg
        })
        //reject(res)
      }
    })
  },
  set_total(e){
    console.log();
    this.setData({
      m_total:e.detail.value
    })
  },
  /**
   * 确认充值
   */
  formSubmit: function (e) {
    var that = this;
    var m_total =this.data.m_total;
    var myreg = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
    if (!myreg.test(m_total)) {
      wx.showModal({
        title: '提示',
        content: '充值金额不正确！'
      })
      return false;
    }
    var paraArr = new Array();
    paraArr['amount'] = m_total;
    paraArr['user_id'] = that.data.userInfo.userID
    var sign = app.signature(paraArr);
    console.log(paraArr)
    wx.request({
      url: rootDocment + '/api/Recharge',
      data: {
        user_id: paraArr['user_id'],
        amount: paraArr['amount'],
        sign: sign
      },
      method: 'POST',
      header: {},
      success: function (res) {
        if (res.data.code == '1001') {
          console.log(res.data.data.sn);
          let sn = res.data.data.sn
          let isbalance = false
          that.setData({
            m_total:0,
            isShow:false
          })
          app.gourl('pay/index', 'type=2&total=' + m_total + '&sn=' + sn + '&isbalance=' + '2');
        } else {
          if (res.data.code == 1003) {
            wx.showModal({
              title: '提示',
              content: res.data.msg || '绑定手机号才可购买',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/shouji/shouji',
                  })
                  
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }

              }
            })

          } else {
            wx.showModal({
              title: '提示',
              content: res.msg
            })
          }
        }

      }
    })
  },

})